#!/usr/bin/env tclsh8.5
# $Id$
# Copyright (c) 2012, Prabhu Gurumurthy
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# Neither the name of the "Any company that I work for" nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# __version__  = "$Revision$"
# __author__   = "$Author$"
# __modified__ = "$Date$"

set progname [lindex [split $argv0 '/'] end]

proc usage {} {
   global progname
   puts "$progname options"
   puts "options: "
   puts "\t  -h | --help : prints this help"
   puts "\t  -d | --dest : directory where netaddr library will be installed"
   exit 1
}

proc optparse { argv argc } {
   global progname
   array set args {}
   set count 0
   foreach element $argv {
      set args($count) $element
      incr count
   }

   set destdir ""

   foreach element [array name args] {
      if {$args($element) == "-h" || $args($element) == "--help"} {
         usage
      } elseif {$args($element) == "-d" || $args($element) == "--dest"} {
         set destdir $args([incr element])
      } else {
         usage
      }
   }

   if {$tcl_platform(user) != "root"} { 
      puts "$progname: cannot proceed, need root privileges to proceed"
      exit 1
   }

   if {[string length $destdir] == 0} {
      if {[file exists $tcl_library] && [file isdirectory $tcl_library]} {
         set destdir [concat "$tcl_library/netaddr"]
      }
   }

   if {[file exists $destdir]} { 
      puts "$destdir exists, skipping directory creation!"
   } else {                      
      file mkdir $destdir        
      puts "$destdir created"    
   }                             
   
   set sourcefile [format "%s/library/netaddr.tcl" [pwd]]
   if {[file exists $sourcefile]} {  
      set temp [concat "$destdir/netaddr.tcl"]
      if {[file exists $temp]} {
         puts "netaddr.tcl already exists within $destdir"
      } else {
         file copy $sourcefile $destdir 
         puts "netaddr.tcl copied to $destdir"
      }

      puts "$sourcefile copied to $destdir" 
      pkg_mkIndex $destdir "*.tcl"
      puts "Created package index"
   } else {
      puts "$sourcefile does not exist, check your download"
      exit 1
   }
} 

optparse $argv $argc
